package com.briup.jdk8;

import java.io.PrintStream;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * Stream 的操作
 */
public class StringFinalOP {
    public static void main(String[] args) {
        Stream<String> stream = Stream.of("海鹏", "海螺", "海星", "海燕", "海绵宝宝");
        //获取迭代器
        Iterator<String> iterator = stream.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        stream = Stream.of("海鹏", "海螺", "海星", "海燕", "海绵宝宝");
//        stream.forEach(i-> System.out.println(i));
        stream.forEach(System.out::println);
        stream = Stream.of("海鹏", "海螺", "海星", "海燕", "海绵宝宝");
        long count = stream.count();
        System.out.println("count = " + count);
        stream = Stream.of("海鹏", "海螺", "海星", "海燕", "海绵宝宝");
//        Optional<String> max = stream.max(new Comparator<String>() {
//            @Override
//            public int compare(String o1, String o2) {
//                return o1.compareTo(o2);
//            }
//        });
//        System.out.println(max.get());
//        Optional<String> max = stream.max((o1, o2) -> o1.compareTo(o2));
//        System.out.println(max.get());
//        Optional<String> max = stream.max(String::compareTo);
//        System.out.println(max.get());
//
        Optional<String> min = stream.min(String::compareTo);
        System.out.println(min.get());
        stream = Stream.of("海鹏", "海螺", "海星", "海燕", "海绵宝宝","海王","海苔","海白菜","海公牛");
        //完全匹配 要求 每一个元素都要满足条件
//        boolean b = stream.allMatch(i -> i.length() >= 3);
        //任意匹配 只要其中一个元素满足条件 就返回true
//        boolean b = stream.anyMatch(i -> i.contains("星"));
        //不匹配   任意元素都不满足条件 返回true
        boolean b = stream.noneMatch(i -> i.length() > 4);
        System.out.println(b);

        stream = Stream.of("海鹏", "海螺", "海星", "海燕", "海绵宝宝","海王","海苔","海白菜","海公牛");
        Optional<String> first = stream.findFirst();
        System.out.println(first.get());


    }

}
